#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <ecgi.h>
#include <xmlsec/app.h>
#include <xmlsec/dl.h>
#include <libxml/xmlsave.h>

#include "prepaid.h"

int main(int argc, char **argv) {
  const char *value;
  char *tmp;
  xmlDocPtr doc;
  xmlSaveCtxtPtr ctxt;
  int index;
  
  assert(cgiInit() == ECGI_SUCCESS);
  xmlInitParser();
  assert(!xmlSecInit());
#ifndef XMLSEC_NO_CRYPTO_DYNAMIC_LOADING
  assert(!xmlSecCryptoDLLoadLibrary("openssl"));
#endif
  assert(!xmlSecCryptoAppInit(NULL));
  assert(!xmlSecCryptoInit());
  cgiSetMaxSize(50000);
  value = cgiParam("pin");
  assert(value);
  index = strtoul(value, &tmp, 10);
  assert(!*tmp);
  doc = pp_getauth(index);
  assert(doc);
  printf("Content-type: text/xml\n\n");
  fflush(stdout);
  ctxt = xmlSaveToFd(1, "UTF-8", XML_SAVE_NO_EMPTY|XML_SAVE_NO_DECL);
  xmlSaveDoc(ctxt, doc);
  xmlSaveClose(ctxt);
  return 0;
}
